﻿@{
    Layout = null;
}
@Html.Hidden("RoleIds")
@Html.Hidden("UserId")
<table id="RoleTable" class="table table-striped table-hover dt-responsive display nowrap" cellspacing="0" width="100%"></table>
<script>
    var RoleIds=[]
    var Roles = {
        Search: function () {
            $("#RoleTable").mainToolDataTable({
                "ajax": {
                    url: '@Url.Action("PageRoles", "Roles")',
                    type: 'post',
                    data: {
                        filter: {
                            Name: "",
                            Code: "",
                        }
                    }
                },
                "columns": [
                    { data: "Name", title: "角色名称" },
                    { data: "Code", title: "角色编码" },
                    {
                        title: "全选<input id='checkAll' type='checkbox' />",
                        orderable: false,
                        data: "Id",
                        style: "min-width:190px;",
                        render: function (data, type, row) {
                            if ($.inArray(data, $.parseJSON($("#RoleIds").val())) >= 0 && $.inArray(data, RoleIds) < 0)
                                RoleIds.push(data);
                            return "<input class='roleIdClass' type='checkbox' value='" + data + "' " + ($.inArray(data, $.parseJSON($("#RoleIds").val())) >= 0 ? "checked='checked'" : "") + " />";
                        }
                    },
                ],
                "preDrawCallback": function () {
                    RoleIds = [];
                },
                "initComplete": function () {
                    $(".roleIdClass").click(function () {
                        $("#checkAll").prop("checked", $("input.roleIdClass").length == $("input.roleIdClass:checked").length ? true : false);
                    });
                    $("#checkAll").prop("checked", $("input.roleIdClass").length == $("input.roleIdClass:checked").length ? true : false);
                }
            });
        },
    };
    Users.SubmitAllocateRoles = function () {
        var selectRoleIds = new Array();
        $(".roleIdClass").each(function (e, item) {
            if (item.checked) {
                selectRoleIds.push(Number($(item).val()));
            }
        });
        $.mainTool.ajax({
            url: '@Url.Action("AllocateRolesSubmit")',
            data: {
                UserId: Number($("#UserId").val()),
                RoleIds: RoleIds,
                SelectRoleIds: selectRoleIds,
            },
            success: function (data) {
                Users.Search();
            }
        });
    };
    $(document).ready(function () {
        Roles.Search();
        $("#checkAll").click(function () {
            $('input.roleIdClass').prop("checked", $(this).prop("checked"));
        });
    });
</script>